package com.mvp.shq.washer.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.TypeChoseAdapter;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.SpUtil;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shq1 on 2017/10/12.
 * 支付类型选择弹出框
 */

public class PayTypePopupWindow extends PopupWindow {


    private View mPopView;
    private Activity activity;
    private ViewHolder holder;
    private String mTitleName;
    private int currerPosition;
    private TypeChoseAdapter adapter;
    private OnCheckListener onCheckListener;
    private View.OnClickListener onClick;
    private String mMoney;
    public int getCurrerPosition() {
        return currerPosition;
    }

    public void setCurrerPosition(int currerPosition) {
        this.currerPosition = currerPosition;
    }

    public PayTypePopupWindow(Context context, Activity mActivity, OnCheckListener onCheckListener,String money,View.OnClickListener onClick) {
        super(context);
        this.activity = mActivity;
        this.onCheckListener = onCheckListener;
        this.mMoney=money;
        this.onClick=onClick;
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
    }

    /**
     * 初始化
     *
     * @param context
     */
    @SuppressLint("WrongViewCast")
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.pop_paytype, null);
        holder = new ViewHolder(mPopView);
        if (!TextUtils.isEmpty(mTitleName)) {
            holder.tvTitlename.setText(mTitleName);
        }
        if(!TextUtils.isEmpty(mMoney)){
            holder.tvYue.setText(mMoney);
        }
        holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    holder.check2.setChecked(false);
                    holder.check3.setChecked(false);
                    currerPosition=0;
                    onCheckListener.onCheck(0);
                }

            }
        });
        holder.check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    holder.check1.setChecked(false);
                    holder.check3.setChecked(false);
                    currerPosition=1;
                    onCheckListener.onCheck(1);
                }
            }
        });
        holder.check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    holder.check1.setChecked(false);
                    holder.check2.setChecked(false);
                    currerPosition=3;
                    onCheckListener.onCheck(2);
                }
            }
        });
        holder.linpay2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.check2.setChecked(true);
                holder.check1.setChecked(false);
                holder.check3.setChecked(false);
                currerPosition=1;
                onCheckListener.onCheck(1);
            }
        });
        holder.linpay3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.check3.setChecked(true);
                holder.check1.setChecked(false);
                holder.check2.setChecked(false);
                currerPosition=2;
                onCheckListener.onCheck(2);
            }
        });
        holder.linpay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.check1.setChecked(true);
                holder.check2.setChecked(false);
                holder.check3.setChecked(false);
                currerPosition=0;
                onCheckListener.onCheck(0);
            }
        });
        holder.bntPay.setOnClickListener(onClick);
        holder.imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });
        User user = SpUtil.getUser();
        if (user != null){
            holder.tvYue.setText(user.getRechargeBalance().add(user.getGivenMoney())+"");
        }

    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.popwindow_anim_bottom);// 设置动画
        this.setBackgroundDrawable(new BitmapDrawable());// 设置背景透明
        //this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }

    public class ViewHolder {
        @BindView(R.id.tv_titlename)
        TextView tvTitlename;
        @BindView(R.id.check1)
        CheckBox check1;
        @BindView(R.id.linpay1)
        LinearLayout linpay1;
        @BindView(R.id.check2)
        CheckBox check2;
        @BindView(R.id.linpay2)
        LinearLayout linpay2;
        @BindView(R.id.check3)
        CheckBox check3;
        @BindView(R.id.linpay3)
        LinearLayout linpay3;
        @BindView(R.id.bnt_pay)
        TextView bntPay;
        @BindView(R.id.id_pop_layout)
        LinearLayout idPopLayout;
        @BindView(R.id.tv_yue)
        TextView tvYue;
        @BindView(R.id.img_close)
        ImageView imgClose;
        public ViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCheckListener {
        void onCheck(int position);
    }
}
